<?php
/**
 * For a running Search Demo see: http://amazonecs.pixel-web.org
 */


require_once('config/constant.php');
require_once(DAEMONE_HOME.'/config/amazon.php');
require_once(DAEMONE_HOME.'/lib/lib_database.php');
require_once(DAEMONE_HOME.'/lib/lib_rss_database.php');
require_once(DAEMONE_HOME.'/lib/lib_helper_function.php');

require_once(DAEMONE_HOME.'/lib/AmazonECS.class.php');


try
{
    $amazonEcs = new AmazonECS(AWS_API_KEY, AWS_API_SECRET_KEY, 'com', AWS_ASSOCIATE_TAG);

    // for the new version of the wsdl its required to provide a associate Tag
    // @see https://affiliate-program.amazon.com/gp/advertising/api/detail/api-changes.html?ie=UTF8&pf_rd_t=501&ref_=amb_link_83957571_2&pf_rd_m=ATVPDKIKX0DER&pf_rd_p=&pf_rd_s=assoc-center-1&pf_rd_r=&pf_rd_i=assoc-api-detail-2-v2
    // you can set it with the setter function or as the fourth paramameter of ther constructor above
    $amazonEcs->associateTag(AWS_ASSOCIATE_TAG);

	$amazon_result = execute_query("select * from amz_products_lookup where is_active=1 limit 400 ");

	if($amazon_result === FALSE) 
	{
		echo  mysql_error();
	}
	
	global $category_id;
	while($row = mysql_fetch_array($amazon_result, MYSQL_ASSOC))
	{
		$category_id = $row['category_id'];
		// Looking up multiple items
		$response = $amazonEcs->responseGroup('ItemAttributes,Offers,EditorialReview,Images,Similarities,Reviews')->optionalParameters(array('Condition' => 'All'))->lookup($row['aff_uid']);

		if($response->Items->Request->IsValid == 'True')
		{
			if (!is_array($response->Items->Item))
			{
				$categoryItemCount ++;
				parseNprocessItem($response->Items->Item, NULL);
			}
		}
		else
		{
			echo 'Items->Request->IsValid not valid';
		}
		sleep(20);
		execute_query("update amz_products_lookup set is_active=0 where aff_uid='".$row['aff_uid']."'");
		//print_r($response);exit;
	}
}
catch(Exception $e)
{
  echo $e->getMessage();
}

execute_query("insert ignore into products_related (product_id, product_id_related) select prt.product_id, pd.product_id from products_related_temp prt inner join products pd on prt.aff_uid_related=pd.aff_uid");